package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.androidkun.xtablayout.XTabLayout;
import com.ljfiction.book.MyView.BadgeView;
import com.ljfiction.book.R;
import com.ljfiction.book.adapter.SimpleFragmentPagerAdapter;
import com.ljfiction.book.fragment.MyFommentFragment;
import com.ljfiction.book.fragment.MyinformFragment;
import com.ljfiction.book.fragment.MyprivateletterFragment;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

    public class MyInformationActivity extends BaseActivity {

    @BindView(R.id.myinformationxtable)
    XTabLayout myinformationxtable;
    @BindView(R.id.informationviewpager)
    ViewPager informationviewpager;



    private ArrayList<Fragment> fragments = new ArrayList<>();
    private String tabTitles[] = new String[]{ "私信", "评论","通知"};
    private int tabImage[] = new int[]{R.mipmap.messagetop, R.mipmap.pingluntop, R.mipmap.tongzhitop};
    private List<Integer> mBadgeCountList = new ArrayList<Integer>();
    private List<BadgeView> mBadgeViews;
    private SimpleFragmentPagerAdapter adapter;
    private MyinformFragment myinformFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_information);
        ButterKnife.bind(this);
        initBadgeViews();
        intview();
        intData();
    }


    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/person-messageCount")

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.getInt("returnCode") == 200) {
                                mBadgeCountList.set(1, jsonObject.getJSONObject("data").getInt("userMessageCount"));
                                mBadgeCountList.set(2, jsonObject.getJSONObject("data").getInt("userCommentReplyCount"));
                                setUpTabBadge();
                            }else if (jsonObject.getInt("returnCode") == 400){
                                Intent intent=new Intent(MyInformationActivity.this, LoginActivity.class);
                                startActivity(intent);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    private void intview() {
        myinformFragment=new MyinformFragment();
        fragments.add(new MyprivateletterFragment());
        fragments.add(myinformFragment);
        fragments.add(new MyFommentFragment());
        myinformFragment.setTorefresh(new MyinformFragment.Torefresh() {
            @Override
            public void refresh() {
                intData();
            }
        });
        mBadgeCountList.add(0);
        mBadgeCountList.add(0);
        mBadgeCountList.add(0);
        adapter = new SimpleFragmentPagerAdapter(this, getSupportFragmentManager(), fragments, tabTitles, mBadgeCountList,tabImage);
        informationviewpager.setAdapter(adapter);
        informationviewpager.setOverScrollMode(View.OVER_SCROLL_NEVER);
        myinformationxtable.setupWithViewPager(informationviewpager);
        setUpTabBadge();
    }

    private void setUpTabBadge() {
        // 1. 最简单
       /* for (int i = 0; i < fragments.size(); i++) {
            mBadgeViews.get(i).setTargetView(((ViewGroup) myinformationxtable.getChildAt(0)).getChildAt(i));
            mBadgeViews.get(i).setText(formatBadgeNumber(mBadgeCountList.get(i)));
        }*/
        // 2. 最实用
        for (int i = 0; i < fragments.size(); i++) {
            XTabLayout.Tab tab = myinformationxtable.getTabAt(i);
            // 更新Badge前,先remove原来的customView,否则Badge无法更新
            View customView = tab.getCustomView();
            if (customView != null) {
                ViewParent parent = customView.getParent();
                if (parent != null) {
                    ((ViewGroup) parent).removeView(customView);
                }
            }
            // 更新CustomView
            tab.setCustomView(adapter.getTabItemView(i,myinformationxtable.getSelectedTabPosition()));
        }
        // 需加上以下代码,不然会出现更新Tab角标后,选中的Tab字体颜色不是选中状态的颜色
        //myinformationxtable.getTabAt(myinformationxtable.getSelectedTabPosition()).getCustomView().setSelected(true);
    }

    private void initBadgeViews() {
        if (mBadgeViews == null) {
            mBadgeViews = new ArrayList<BadgeView>();
            for (int i = 0; i < 3; i++) {
                BadgeView tmp = new BadgeView(this);
                tmp.setBadgeMargin(0, 6, 10, 0);
                tmp.setTextSize(10);
                mBadgeViews.add(tmp);
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        intData();
    }

}
